import request from '@/utils/request'

/**
 * 批量引入所有api.js文件
 */
const allApis = (() => {
  // 引入api文件夹下的api接口
  const requireAll = require.context('@/api/apis', false, /\.js$/)
  // requireAll.keys()为文件名数组; requireAll(apiName)获取文件暴露的内容
  const apiArr = requireAll.keys().map((apiName) => {
    // 提取文件名作为命名空间
    const namespace = apiName.replace(/^\.\/(.+)\.js$/, '$1')
    // 获取文件导出的内容
    const apiContent = requireAll(apiName).default || requireAll(apiName)
    // 返回带有命名空间的对象
    return { namespace, content: apiContent }
  })

  const apis = apiArr.reduce((prev, curr) => {
    prev[curr.namespace] = curr.content
    return prev
  }, {})
  return apis
})()


// 导出所有api方法
export default {
  noticeList(data) {
    return request({
      url: '/energy-carbon/grass-web/notice/list',
      method: 'post',
      data
    })
  },
  updatePassword(data) {
    return request({
      url: '/energy-carbon/grass-web/web/updatePassword',
      method: 'post',
      data
    })
  },
  login(data) {
    return request({
      url: '/energy-carbon/grass-web/web/login',
      method: 'post',
      data
    })
  },
  getInfo(token) {
    return request({
      url: '/energy-carbon/grass-web/web/getLoginUserInfo',
      method: 'post',
      params: { token }
    })
  },
  logout() {
    return request({
      url: '/energy-carbon/grass-web/web/logout',
      method: 'post'
    })
  },
  // 模板下载
  downTpl(data) {
    return request({
      url: '/energy-carbon/grass-web/loginLog/export',
      method: 'get',
      params: data,
      responseType: 'blob'
    })
  },
  // 筛选列表
  select(data) {
    return request({
      url: '/energy-carbon/grass-web/web/getDict',
      method: 'post',
      data
    })
  },
  // 字典
  dict(data) {
    return request({
      url: '/energy-carbon/grass-core/common/dict',
      method: 'post',
      data
    })
  },
  // 字典（批量）
  dictList(data) {
    return request({
        url: '/energy-carbon/grass-core/common/dictList',
      method: 'post',
      data
    })
  },
  // 公共下拉数据
  commonSelected(data) {
    return request({
      url: '/energy-carbon/grass-core/common/selected',
      method: 'post',
      data
    })
  },
  // 公共下拉数据
  selectedOne(data) {
    return request({
      url: '/energy-carbon/grass-core/common/selectedOne',
      method: 'post',
      data
    })
  },
  // 公共下拉数据集合
  selectedGroup(data) {
    return request({
      url: '/energy-carbon/grass-core/common/selectedGroup',
      method: 'post',
      data
    })
  },
  // 公共下拉数据
  selectedList(data) {
    return request({
      url: '/energy-carbon/grass-core/common/selectedList',
      method: 'post',
      data
    })
  },
  // 组织架构
  deptTree(data) {
    return request({
      url: '/energy-carbon/grass-core/dept/queryDeptList',
      method: 'post',
      data
    })
  },
  // 所有用户
  queryUserList(data) {
    return request({
      url: '/energy-carbon/grass-core/user/queryUserList',
      method: 'post',
      data
    })
  },

  // 切换角色
  changeRole(data) {
    return request({
      url: '/energy-carbon/grass-web/web/changeRole',
      method: 'post',
      data
    })
  },
  // 是否开启租户
  enableTenant(data) {
    return request({
      url: '/energy-carbon/grass-web/web/enableTenant',
      method: 'post',
      data
    })
  },
  // 用户信息
  getUserCenterInfo(data) {
    return request({
      url: '/energy-carbon/grass-web/web/getUserCenterInfo',
      method: 'post',
      data
    })
  },
  // 更新用户信息
  updateUserCenterInfo(data) {
    return request({
      url: '/energy-carbon/grass-web/web/updateUserCenterInfo',
      method: 'post',
      data
    })
  },
  // 项目树
  projectTree(data) {
    return request({
      url: '/energy-carbon/ecp-admin/sceneProject/queryList',
      method: 'post',
      data
    })
  },
  edificeAndProjectTree(data) {
    return request({
      url: '/energy-carbon/ecp-admin/sceneEdifice/queryTree',
      method: 'post',
      data
    })
  },
  floorAndEdificeAndProjectTree(data) {
    return request({
      url: '/energy-carbon/ecp-admin/sceneFloor/queryTree',
      method: 'post',
      data
    })
  },
  roomAndFloorAndEdificeAndProjectTree(data) {
    return request({
      url: '/energy-carbon/ecp-admin/sceneRoom/queryTree',
      method: 'post',
      data
    })
  },
  companyAndRoomAndFloorAndEdificeAndProjectTree(data) {
    return request({
      url: '/energy-carbon/ecp-admin/sceneCompany/queryTree',
      method: 'post',
      data
    })
  },
  ...allApis
}
